/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package memsim;
import java.io.*;
import java.util.*;
/**
 *
 * @author kimchouSok
 */
public class Assembler {

    public FileOutputStream Out;
    public void readFile(String s)throws IOException{
        String line;
        try{
             BufferedReader br = new BufferedReader(new FileReader(s));
             while((line = br.readLine())!=null){
                processLine(line);
             }
            br.close();
        }catch(IOException e){
            System.out.println("File not found");
        }
       
    }

    public void outputFile()throws IOException{
        try{
            Out = new FileOutputStream("output.txt");
        }catch(IOException e){
            System.out.println("ERROR");
        }
    }

    public void processLine(String line){
        String r0  = "11110",
               r1 = "11111",
               r2 = "11101",
               r3 = "10101",
               r4 ="11000",
               r5 ="10101";
        String w ="";
        String word ="";
        
         StringTokenizer tokens = new StringTokenizer(line,", ");
         while(tokens.hasMoreTokens()){
             word = tokens.nextToken();
             if(word.equals("ADDS")){
                  String adds = "000000";
                  w+= adds;
                  
             }else if(word.equals("LDR")){
                 String ld = "000100";
                 w+= ld;
             }else if(word.equals("STRS")){
                 String sts = "000110";
                 w+= sts;
                
             }else if(word.equals("BR")){
	          String br = "000111";
                  w+= br;
	          
	    }else if(word.equalsIgnoreCase("r0")){
                w+= r0;
                 
            }else if(word.equalsIgnoreCase("R1")){
	           w+= r1;
	         
	     }else if(word.equalsIgnoreCase("R2")){
	             w+= r2;
	     }else if(word.equalsIgnoreCase("R3")){
	             w+= r3;
	     }else if(word.equalsIgnoreCase("R4")){
	             w+= r4;
	     }else if(word.equalsIgnoreCase("R5")){
	             w+= r5;
	     }
         }
         new PrintStream(Out).print(w);
         new PrintStream(Out).println();
    }

}
